<?php
// +----------------------------------------------------------------------
//            -------------------------
//           /   / ----------------\  \
//          /   /             \  \
//         /   /              /  /
//        /   /    /-------------- /  /
//       /   /    /-------------------\  \
//      /   /                   \  \
//     /   /                     \  \
//    /   /                      /  /
//   /   /      /----------------------- /  /
//  /-----/      /---------------------------/
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://baimifan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: Sage 2016/4/19  14:05
// +----------------------------------------------------------------------
namespace Wechat\Controller;
use Think\Controller;
//积分
Class ScoreController extends BaseController{

    function index(){
        $user_id=session("user_id");
        $Score=D("Score");
        $d=$Score->where("user_id=$user_id")->field('sum(score) as score,type')->group('type')->select();
        $score=[];
        foreach($d as $k=>$v){
            $score[$d[$k]['type']]['score']=$d[$k]['score'];
        }
        $total=($score[1]['score']+$score[2]['score']+$score[3]['score']+$score[4]['score']);
        $score[1]["percent"]=round(($score[1]['score']+$score[2]['score'])/$total*100,2);
        $score[3]["percent"]=round(($score[3]['score'])/$total*100,2);
        $score[4]["percent"]=round(($score[4]['score'])/$total*100,2);
        if(($score[4]["percent"]+$score[3]["percent"]+$score[1]["percent"]) != 100){
            $score[4]["percent"]=100-$score[1]["percent"]-$score[3]["percent"];
        }
        $score['total']=$Score->total($user_id);
        $this->assign("score",$score);
        $this->display();
    }
    //积分
    function exchange(){
        $this->display();
    }
    // 商城
    public function mall()
    {
        $map['stock'] = ['gt',0];
        $map['_string'] = 'deleted_at is null';
        $list = M('product')->where($map)->order('product_id desc')->select();
        $this->list = $list;
        $this->display();
    }

    public function mallHistory()
    {
        $list = M('order_has_product')->join('product on product.product_id = order_has_product.product_id')
            ->where(['user_id'=>session('user_id')])->order('create_time desc')->select();
        $this->list = $list;
        $this->display();
    }
    public function makeExchange()
    {
        $id = I('id');
        $product = M('product')->where(['product_id' => $id])->find();
        $user_id = session('user_id');
        $Score = D('Score');
        $user_score = $Score->total($user_id);
        if ($product['stock'] <= 0) {
            $this->ajaxReturn(['code' => 1, 'msg' => '商品已经被兑光了']);
        }
        if ($product['price'] > $user_score) {
            $this->ajaxReturn(['code' => 1, 'msg' => '积分不足']);
        }
        $exchange = M('order_has_product');
        // todo 每月兑换次数限制
//        $last= date('Y-m-01 00:00:00');
//        $mapTime['_string'] = 'create_time >"'.$last.'" and create_time < "'.date('Y-m-d H:i:s').'"';
//        $mapTime['user_id'] = $user_id;
//        $result = $exchange->where($mapTime)->find();
//        if ($result) {
//            $this->ajaxReturn(['code' => 1, 'msg' => '兑换失败，每月仅限兑换一次']);
//        }
        $data = [
            'user_id' => $user_id,
            'product_id' => $id,
            'create_time' => date('Y-m-d H:i:s')
        ];
        // 如果出问题换成事务
        $result = $exchange->add($data);
        M('product')->where(['product_id' => $id])->setField('stock',$product['stock']-1);
        D('Score')->exchange($user_id, $product['price']);
        if ($result) {
            $this->ajaxReturn(['code' => 0, 'msg' => '兑换成功，将会在下次购书或借阅物流中配送']);
        } else {
            $this->ajaxReturn(['code' => 1, 'msg' => '兑换失败，请刷新重试']);
        }
    }
}